package com.web.dao;

import com.web.entity.RegionRecord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

/**
 * @author FDjavaone
 */
public interface RegionRecordMapper {
    @Delete({
        "delete from t_region_record",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into t_region_record (u_id, level, ",
        " province, city, ",
        " region, create_time, ",
        " modify_time)",
        " values (#{uId,jdbcType=BIGINT}, #{level,jdbcType=INTEGER}, ",
        " #{province,jdbcType=VARCHAR}, #{city,jdbcType=VARCHAR}, ",
        " #{region,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, ",
        " #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(RegionRecord record);

    @InsertProvider(type=RegionRecordSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(RegionRecord record);

    @Select({
        "select",
        " id, u_id, level, province, city, region, create_time, modify_time",
        " from t_region_record",
        " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="u_id", property="uId", jdbcType=JdbcType.BIGINT),
        @Result(column="level", property="level", jdbcType=JdbcType.INTEGER),
        @Result(column="province", property="province", jdbcType=JdbcType.VARCHAR),
        @Result(column="city", property="city", jdbcType=JdbcType.VARCHAR),
        @Result(column="region", property="region", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    RegionRecord selectByPrimaryKey(Long id);

    @UpdateProvider(type=RegionRecordSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(RegionRecord record);

    @Update({
        "update t_region_record",
        " set u_id = #{uId,jdbcType=BIGINT},",
          " level = #{level,jdbcType=INTEGER},",
          " province = #{province,jdbcType=VARCHAR},",
          " city = #{city,jdbcType=VARCHAR},",
          " region = #{region,jdbcType=VARCHAR},",
          " create_time = #{createTime,jdbcType=TIMESTAMP},",
          " modify_time = #{modifyTime,jdbcType=TIMESTAMP}",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(RegionRecord record);


    @Select({
            "select",
            " id, u_id, level, province, city, region, create_time, modify_time",
            " from t_region_record",
            " where province = #{province,jdbcType=VARCHAR} and level=1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="u_id", property="uId", jdbcType=JdbcType.BIGINT),
            @Result(column="level", property="level", jdbcType=JdbcType.INTEGER),
            @Result(column="province", property="province", jdbcType=JdbcType.VARCHAR),
            @Result(column="city", property="city", jdbcType=JdbcType.VARCHAR),
            @Result(column="region", property="region", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    RegionRecord selectProvince(@Param("province") String province);


    @Select({
            "select",
            " id, u_id, level, province, city, region, create_time, modify_time",
            " from t_region_record",
            " where province = #{province,jdbcType=VARCHAR} and city = #{city,jdbcType=VARCHAR} and level=2"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="u_id", property="uId", jdbcType=JdbcType.BIGINT),
            @Result(column="level", property="level", jdbcType=JdbcType.INTEGER),
            @Result(column="province", property="province", jdbcType=JdbcType.VARCHAR),
            @Result(column="city", property="city", jdbcType=JdbcType.VARCHAR),
            @Result(column="region", property="region", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    RegionRecord selectProvinceAndCity(@Param("province")String province, @Param("city")String city);

    @Select({
            "select",
            " id, u_id, level, province, city, region, create_time, modify_time",
            " from t_region_record",
            " where province = #{province,jdbcType=VARCHAR} and city = #{city,jdbcType=VARCHAR} and region = #{region,jdbcType=VARCHAR} and level=3"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="u_id", property="uId", jdbcType=JdbcType.BIGINT),
            @Result(column="level", property="level", jdbcType=JdbcType.INTEGER),
            @Result(column="province", property="province", jdbcType=JdbcType.VARCHAR),
            @Result(column="city", property="city", jdbcType=JdbcType.VARCHAR),
            @Result(column="region", property="region", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    RegionRecord selectProvinceAndCityAndRegion(@Param("province")String province, @Param("city")String city,@Param("region") String region);

    @Select({
            "select count(0) from t_region_record"
    })
    Integer countTotal();

    @Select({
            "select",
            " id, u_id, level, province, city, region, create_time, modify_time",
            " from t_region_record",
            " where u_id=#{aLong,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="u_id", property="uId", jdbcType=JdbcType.BIGINT),
            @Result(column="level", property="level", jdbcType=JdbcType.INTEGER),
            @Result(column="province", property="province", jdbcType=JdbcType.VARCHAR),
            @Result(column="city", property="city", jdbcType=JdbcType.VARCHAR),
            @Result(column="region", property="region", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    RegionRecord selectByUid(@Param("aLong")Long aLong);
}